<?php
if(!defined('MY_ROOT'))
{throw new Exception('Access Denied');}
require_once(MY_ROOT.'/source/class/simple_html_dom.php');
class opac
{
	private static $curlset=array(CURLOPT_AUTOREFERER=>1,CURLOPT_RETURNTRANSFER=>1,CURLOPT_PORT=>8080,
CURLOPT_USERAGENT=>'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36');
	public static function search($name,$by='title'){
	$url='58.40.126.44/opac/search?rows=10&searchWay='.$by.'&q='.$name;
	$ch=curl_init();
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_setopt_array($ch,self::$curlset);
	$content=curl_exec($ch);
	curl_close($ch);unset($ch);
	$start=strripos($content,'<div class="resultList">');
	$end=strripos($content,'<div class="meneame">');
	$content=substr($content,$start+24,$end-$start-35);
	$htobj=str_get_html($content);
	$books=$htobj->find('div.bookmeta');
	$i=0;
	$mh = curl_multi_init();
	foreach($books as $book){
		$bk[$i]['id']=$book->bookrecno;
		$bk[$i]['title']=trim($book->find('span.bookmetaTitle',0)->children(0)->innertext);
		$bk[$i]['author']=trim($book->children(1)->children(0)->innertext);
		$bk[$i]['publisher']=trim($book->children(2)->children(0)->innertext);
		$ch[$i]=curl_init();
		curl_setopt($ch[$i],CURLOPT_URL,'http://58.40.126.44:8080/opac/book/holdingpreview/'.trim($book->bookrecno));
		curl_setopt_array($ch[$i],self::$curlset);
		curl_multi_add_handle($mh,$ch[$i]);
		$i++;
	}
	$active = null;
	// 执行批处理句柄
	do {
    	$mrc = curl_multi_exec($mh, $active);
		} while ($mrc == CURLM_CALL_MULTI_PERFORM);

	while ($active && $mrc == CURLM_OK) {
    	if (curl_multi_select($mh) != -1) {
        	do {
            	$mrc = curl_multi_exec($mh, $active);
        	} while ($mrc == CURLM_CALL_MULTI_PERFORM);
    	}
	}
	error_reporting(0);
	for($j=0;$j<$i;$j++){
		$content=curl_multi_getcontent($ch[$j]);
		curl_multi_remove_handle($mh, $ch[$j]);
		$xml=simplexml_load_string($content,'SimpleXMLElement', LIBXML_NOCDATA);
		$rec=get_object_vars($xml->record);
		$bk[$j]['callno']=trim($rec['callno']);
		$bk[$j]['place']=trim($rec['curlocalName']);
		$bk[$j]['copy']=trim($rec['copycount']);
	}
	curl_multi_close($mh);
	return $bk;
	}
	public static function bookinfo($recno){
		$ch=curl_init();
		$recno=intval($recno);
		curl_setopt($ch,CURLOPT_URL,'http://58.40.126.44:8080/opac/book/'.$recno);
		curl_setopt_array($ch,self::$curlset);
	}
	public static function rentbook($no){
		$ch=curl_init();
		$str1=date("Y-m-d H:i:s");
		$str2=md5($no.$str1."f54d48bae4282389688919e34e470cab");
		$url="http://m.shmtu.edu.cn/mobile/hsydpt/sjjk/getTSJYs";
		$parm="stuempno=".$no."&sign=".$str2."&sendtime=".$str1."&pageNo=1&pageSize=67";
		curl_setopt($ch,CURLOPT_URL,$url."?".$parm);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
		$res=curl_exec($ch);
		$r=json_decode($res);
		return $r;
	}
	
}